﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using VideoServer.Helpers;
using VideoServer.ViewModel.Helpers;
using System.ComponentModel;

namespace VideoServer.ViewModel
{
    public class MainWindowViewModel:ViewModelBase
    {
        //启动关闭服务显示内容
        private string _startServerContent = "启动服务";
        //本地监听端口
        private string _localPort = string.Empty;
        private RelayCommand startCommand = null;
        private string[] validProperties = new string[] { "LocalPort" };
        private MyUDPServer server = null;
        private DataParseViewModel _dataParseVM = null;
        private LogOutputViewModel _logOutputVM = null;
        private SystemSettingViewModel _systemSettingVM = null;
        bool isStart = false;
        //数据解析窗口实例属性
        public DataParseViewModel DataParseVM
        {
            get
            {
                if (_dataParseVM == null)
                    _dataParseVM = new DataParseViewModel();
                return _dataParseVM;
            }
        }

        //异常信息答应窗口实例属性
        public LogOutputViewModel LogOutputVM
        {
            get
            {
                if (_logOutputVM == null)
                {
                    _logOutputVM = new LogOutputViewModel();
                    ErrorHelper.printLog += _logOutputVM.PrintLog;
                }
                return _logOutputVM;
            }
        }
        //系统设置窗口实例属性
        public SystemSettingViewModel SystemSettingVM
        {
            get
            {
                if (_systemSettingVM == null)
                    _systemSettingVM = new SystemSettingViewModel();
                return _systemSettingVM;
            }
        }
        //本地IP
        public string LocalIp
        {
            get
            {
                return GetLocalIp().ToString();
            }
        }

        public bool IsValid
        {
            get
            {
                foreach (string property in validProperties)
                    if (GetValidatonError(property) != null)
                        return false;
                return true;
            }
        }

        public RelayCommand StartCommand
        {
            get
            {
                if (startCommand == null)
                    startCommand = new RelayCommand(param => Start(), param => canStart());
                return startCommand;
            }
        }

        public string Error
        {
            get
            {
                return null;
            }
        }

        public string StartServerContent
        {
            get
            {
                return _startServerContent;
            }
        }

        public string LocalPort
        {
            get
            {
                return _localPort;
            }

            set
            {
                _localPort = value;
            }
        }

        //错误验证
        public string this[string propertyName]
        {
            get
            {
                return this.GetValidatonError(propertyName);
            }
        }

        private bool canStart()
        {
            return this.IsValid;
        }
        //启动服务
        private void Start()
        {
            if (_startServerContent == "启动服务")
            {
                server = new MyUDPServer(int.Parse(_localPort), _dataParseVM);
                server.StartServer();
                isStart = true;
                _startServerContent = "停止服务";
                OnPropertyChanged("StartServerContent");
            }
            else
            {
                server.StopServer();
                isStart = false;
                _startServerContent = "启动服务";
                OnPropertyChanged("StartServerContent");
            }
        }
       
        //获取本机IP地址
        private IPAddress GetLocalIp()
        {
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            IPAddress ip = (from i in ips where i.AddressFamily == AddressFamily.InterNetwork select i).First();
            return ip;
        }
        //属性值验证
        private string GetValidatonError(string propertyName)
        {
            if (Array.IndexOf(validProperties, propertyName) < 0)
                return null;
            string error = null;
            switch (propertyName)
            {
                case "LocalPort":error = this.ValidLocalPort();break;
                default: break;
            }
            return error;
        }

        private string ValidLocalPort()
        {
            int port;
            if (int.TryParse(_localPort, out port))
            {
                return null;
            }
            else
            {
                return "本地监听端口号错误";
            }
        }

        //窗口关闭事件
        internal void window_Closing(object sender, CancelEventArgs e)
        {
            if(isStart)
                server.StopServer();
        }
    }
}
